package com.lqr.wechat.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;

import com.bridge.imconfig.NimUserInfoSDK;
import com.lqr.wechat.R;
import com.lqr.wechat.databinding.ActivityChangeSignatureBinding;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.uinfo.constant.UserInfoFieldEnum;
import java.util.HashMap;
import java.util.Map;

/**
 * @创建者 CSDN_LQR
 * @描述 更改个性签名
 */
public class ChangeSignatureActivity extends BaseActivity {

    private String mSignature;
    private ActivityChangeSignatureBinding binding ;

    @Override
    protected int layoutId() {
        return R.layout.activity_change_signature;
    }

    @Override
    public void init() {
        mSignature = getIntent().getStringExtra("signature");
    }

    @Override
    public void initView() {
        binding = getBind() ;
        initToolbar();
        binding.etName.setText(mSignature);
        binding.etName.setSelection(mSignature.length());
        binding.tvCount.setText(String.valueOf(30 - binding.etName.getText().toString().length()));
    }

    @Override
    public void initListener() {
        binding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                binding.tvCount.setText(String.valueOf(30 - binding.etName.getText().toString().length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.lyToolbar.btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = binding.etName.getText().toString();
                showWaitingDialog("请稍等");
                Map<UserInfoFieldEnum, Object> fields = new HashMap<>(1);
                fields.put(UserInfoFieldEnum.SIGNATURE, name);
                NimUserInfoSDK.updateUserInfo(fields, new RequestCallbackWrapper<Void>() {
                    @Override
                    public void onResult(int code, Void result, Throwable exception) {
                        hideWaitingDialog();
                        finish();
                    }
                });
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initToolbar() {
        setSupportActionBar(binding.lyToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("更改名字");
        binding.lyToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        binding.lyToolbar.btnOk.setVisibility(View.VISIBLE);
    }

}
